package operation;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class FindAllOperationJDBC implements IOperation{
    @Override
    public void work() throws SQLException {
        System.out.println("查找全部学生消息");
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java110?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("zkzsa123");
        Connection connection = dataSource.getConnection();
        String sql = "select * from student";

        PreparedStatement statement = connection.prepareStatement(sql);
        ResultSet rs = statement.executeQuery();
        int i = 0;
        while(rs.next()){
            String studentId = rs.getString("studentId");
            String studentName = rs.getString("studentName");
            int age = rs.getInt("age");
            String sex = rs.getString("sex");
            String id = rs.getString("id");
            String telephone = rs.getString("telephone");
            System.out.println("Student{" +
                    "studentId=" + studentId +
                    ", studentName='" + studentName + '\'' +
                    ", age=" + age +
                    ", sex='" + sex + '\'' +
                    ", Id='" + id + '\'' +
                    ", telephone='" + telephone + '\'' +
                    '}');
            i++;
        }
        if(i == 0){
            System.out.println("学生表为空,无法查看");
        }
        rs.close();
        statement.close();
        connection.close();


    }
}
